package com.macro.mall.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsPromotionalImgParam;
import com.macro.mall.mapper.BmsPromotionalImgMapper;
import com.macro.mall.model.BmsPromotionalImg;
import com.macro.mall.model.BmsPromotionalImgExample;
import com.macro.mall.service.BmsPromotionalImgService;
import com.macro.mall.utils.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品属性管理Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class BmsPromotionalImgServiceImpl implements BmsPromotionalImgService {
    @Autowired
    private BmsPromotionalImgMapper bmsPromotionalImgMapper;
    @Autowired
    private UserUtils userUtils;
    /**
     * 分页查询轮播图
     * @param
     * @param
     * @param
     * @param
     * @return
     */
    @Transactional
    @Override
    public List<BmsPromotionalImg> getList(BmsPromotionalImgParam bmsPromotionalImgParam) {
        PageHelper.startPage(bmsPromotionalImgParam.getPageNum(), bmsPromotionalImgParam.getPageSize());
        BmsPromotionalImgExample example = new BmsPromotionalImgExample();
        BmsPromotionalImgExample.Criteria criteria = example.createCriteria();
        criteria.andIsDelete(0);
        example.setOrderByClause("sort desc, create_time desc");
        return bmsPromotionalImgMapper.selectByExample(example);
    }

    /**
     * 新增宣传图
     * @param
     * @return
     */
    @Transactional
    @Override
    public int create(BmsPromotionalImgParam bmsPromotionalImgParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsPromotionalImg bmsPromotionalImg = new BmsPromotionalImg();
        BeanUtils.copyProperties(bmsPromotionalImgParam, bmsPromotionalImg);
        bmsPromotionalImg.setCreateTime(new DateTime());
        bmsPromotionalImg.setUpdateTime(new DateTime());
        bmsPromotionalImg.setCreateUser(userId);
        bmsPromotionalImg.setUpdateUser(userId);
        int count = bmsPromotionalImgMapper.insertSelective(bmsPromotionalImg);
        return count;
    }


    /**
     * 更新轮播图
     * @param bmsPromotionalImgParam
     * @return
     */
    @Transactional
    @Override
    public int update(BmsPromotionalImgParam bmsPromotionalImgParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsPromotionalImg bmsPromotionalImg = new BmsPromotionalImg();
        BeanUtils.copyProperties(bmsPromotionalImgParam, bmsPromotionalImg);
        bmsPromotionalImg.setId(bmsPromotionalImgParam.getId());
        bmsPromotionalImg.setUpdateTime(new DateTime());
        bmsPromotionalImg.setUpdateUser(userId);
        return bmsPromotionalImgMapper.updateByPrimaryKeySelective(bmsPromotionalImg);
    }

    /**
     * 获取单个宣传图信息
     * @param id
     * @return
     */
    @Override
    public BmsPromotionalImg getItem(Long id) {
        return bmsPromotionalImgMapper.selectByPrimaryKey(id);
    }

    /**
     * 删除轮播图
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public int delete(List<Long> ids) {
        BmsPromotionalImgExample example = new BmsPromotionalImgExample();
        example.createCriteria().andIdIn(ids);
        Long userId = userUtils.getUserId();
        example.createCriteria().andUpdateUserEqualTo(userId);
        example.createCriteria().andUpdateTimeEqualTo(new DateTime());
        example.createCriteria().andIsDelete(1);
        int count = bmsPromotionalImgMapper.deleteByExample(example);
        return count;
    }
    @Transactional
    @Override
    public int updateShowStatus(BmsPromotionalImgParam bmsPromotionalImgParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsPromotionalImg bmsPromotionalImg = new BmsPromotionalImg();
        BeanUtils.copyProperties(bmsPromotionalImgParam, bmsPromotionalImg);
        bmsPromotionalImg.setId(bmsPromotionalImgParam.getId());
        bmsPromotionalImg.setIsEnabled(bmsPromotionalImgParam.getIsEnabled());
        bmsPromotionalImg.setUpdateTime(new DateTime());
        bmsPromotionalImg.setUpdateUser(userId);
        return bmsPromotionalImgMapper.updateByPrimaryKeySelective(bmsPromotionalImg);
    }

    /**
     * 小程序查询宣传图
     * @return
     */
    @Transactional
    @Override
    public List<BmsPromotionalImg> getMpList() {
        BmsPromotionalImgExample example = new BmsPromotionalImgExample();
        BmsPromotionalImgExample.Criteria criteria = example.createCriteria();
        criteria.andIsDelete(0).andIsEnabled(1);
        example.setOrderByClause("sort desc, create_time desc");
        return bmsPromotionalImgMapper.selectByExample(example);
    }
}
